﻿
package bean.reflection.basic;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * You have to read TestPropertyDescriptor.java and TestInvokeReadMethod.java first.<br>
 * 測試由 Reflection 機制, 啟動 Write Method<br>
 * <br>
 * Copyright: Copyright (c) 2001-2007<br>
 * Company: 一零四資訊科技股份有限公司
 * 
 * @author Eric.Kao(eric.kao@104.com.tw)
 * @version 1.0
 */
public class TestInvokeWriteMethod
{
	PropertyDescriptor descriptors[];

	MyBean myBean;

	public TestInvokeWriteMethod()
		throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
	{
		myBean = new MyBean();

		descriptors = Introspector.getBeanInfo( myBean.getClass() ).getPropertyDescriptors();

		System.out.println( "********** Test Write Value *********" );

		// set name property
		invokeMethodForSetName( searchWriteMethod( "name", String.class ) );

		// set selected
		invokeMethodForIsSelected( searchWriteMethod( "selected", boolean.class ) );

		// print java bean value
		System.out.println();
		System.out.println( "============ Java Bean value ===============" );
		System.out.println( "Name:" + myBean.getName() );
		System.out.println( "Selected:" + myBean.isSelected() );
	}

	public static void main( String[] args )
	{
		try
		{
			new TestInvokeWriteMethod();
		}
		catch( Exception ex )
		{
			ex.printStackTrace();
		}
	}

	/**
	 * Get write method by property name and return type
	 * 
	 * @param property String property name
	 * @param returnType Class property type
	 * @return Method It's Read Method
	 */
	private Method searchWriteMethod( String propertyName, Class returnType )
		throws IntrospectionException
	{
		for( int i = 0; i < descriptors.length; i++ )
		{
			PropertyDescriptor descriptor = descriptors[ i ];

			if( descriptor.getName().equals( propertyName ) && ( descriptor.getPropertyType() == returnType ) )
			{
				return descriptor.getWriteMethod();
			}
		}

		throw new IntrospectionException( "無法尋找到指定的變數名稱" );
	}

	/**
	 * invoke method SetName()
	 * 
	 * @param method
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	private void invokeMethodForSetName( Method method )
		throws InvocationTargetException, IllegalArgumentException, IllegalAccessException
	{
		Object[] args =
			{ "eric gau" };

		// 因為 MyBean.setName() 有一個參數
		Object returnValue = method.invoke( myBean, args );

		System.out.println( "return value for method setName()(Correct Value is null):" + returnValue );
	}

	/**
	 * invoke method isSelected()
	 * 
	 * @param method
	 * @throws InvocationTargetException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	private void invokeMethodForIsSelected( Method method )
		throws InvocationTargetException, IllegalArgumentException, IllegalAccessException
	{
		Object[] args =
			{ new Boolean( true ) };

		// Because MyBean.setSelected() has a argument.
		Object result = method.invoke( myBean, args );

		System.out.println( "return value for method setSelected()(Correct Value is null):" + result );
	}

}